package com.resou.reader.reader.end;

import android.app.Activity;
import android.text.TextUtils;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BaseComment;
import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.api.entry.CommonData;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.BookService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.commom.Constant;
import com.resou.reader.mine.IView.UserInstance;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.ResourceObserver;

public class EndBookPresenter extends ResouBasePresenter<ContractEndBookInterface.IEndBookView> implements ContractEndBookInterface.IEndBookPresenter {
    BookService bookService;

    public EndBookPresenter(ContractEndBookInterface.IEndBookView view) {
        super(view);
        bookService = ApiImp.getInstance().getService(BookService.class);
    }


    /**
     * 获取书籍详情
     *
     * @param bookId
     */
    @Override
    public void quearRecommentBookList(String bookId) {
        mView.showProgress();
        bookService.quearRecommendBookList(bookId).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<BookDetailBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<BookDetailBean>> listResult) {
                        mView.recommentBook(listResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    /**
     * 获取评论数量
     *
     * @param bookId
     */
    @Override
    public void quearCommentNum(String bookId) {
        bookService.reviewComment(bookId, "", 0, 0).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<BaseComment>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(BaseComment baseComment) {
                        mView.showCommentNum(baseComment.getDataSize() + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void quearBookDetail(String bookId) {
        bookService.getBookDetail(UserInstance.getToken(), bookId, Constant.APP, "")
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<CommonData<BookDetailBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<CommonData<BookDetailBean>> listResult) {
                        mView.showBookDetail(listResult.getData().getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void share(Activity activity, String shareUrl, BookDetailBean bookDetailBean, UMShareListener shareListener) {
        if (TextUtils.isEmpty(shareUrl)) {
            shareUrl = "https://m.resouxs.com/bookDetail/" + bookDetailBean.getId();
        } else {
            shareUrl = shareUrl.replace("bookDetails", "bookDetail").replace("testpc", "m");
        }
//        UMWeb web = new UMWeb(shareUrl);
//        web.setTitle(bookDetailBean.getNovelName() + "");//标题
//        web.setDescription(bookDetailBean.getNovelDescription());//描述
        UMWeb web = new UMWeb(shareUrl);
        web.setTitle("平台补贴，海量正版书籍免费看！");//标题
        web.setDescription("我正在看《" + bookDetailBean.getNovelName() + "》" + bookDetailBean.getNovelDescription() + "");
        web.setThumb(new UMImage(activity, bookDetailBean.getCoverUrl()));
        new ShareAction(activity).withMedia(web).setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE).setCallback(shareListener).open();
    }

}
